/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package agb.servlets;



import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Usuario
 */
public class SrvDescargar extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
      PrintWriter out = response.getWriter();
        
        try 
        {
         
        }
        catch(Exception e)
            {
            }
        finally 
        {            
           
        }
        
       
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        try 
        {
            
               String nomFile = request.getParameter("archivo");
              
               FileInputStream archivo = new FileInputStream("D:" + File.separatorChar + "SistemaWebAGB" + File.separatorChar + "descargas"+nomFile );
              
               int longitud = archivo.available();
               byte[] datos = new byte[longitud];
               archivo.read(datos);
               archivo.close();
               response.setContentType("application/octet-stream");
               response.setHeader("Content-Disposition","attachment;filename="+nomFile);
               ServletOutputStream ouputStream = response.getOutputStream();
               ouputStream.write(datos);
               ouputStream.flush();
             
               ouputStream.close();
            
           
        }
        catch(Exception e)
            {
                System.out.println("ERROR: "+e.getMessage()+" e: "+e.getLocalizedMessage()+"--"+e.toString());
                 
            }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
         
    //  PrintWriter out = response.getWriter();
       /* 
        try 
        {
            
               String nomFile = request.getParameter("archivo");
              
               FileInputStream archivo = new FileInputStream("D:" + File.separatorChar + "SistemaWebAGB" + File.separatorChar + "descargas"+nomFile );
                System.out.println("Archivo: "+"D:\\SistemaWebAGB\\descargas"+nomFile);
               int longitud = archivo.available();
               byte[] datos = new byte[longitud];
               archivo.read(datos);
               archivo.close();
               response.setContentType("application/octet-stream");
               response.setHeader("Content-Disposition","attachment;filename="+nomFile);
               ServletOutputStream ouputStream = response.getOutputStream();
               ouputStream.write(datos);
               ouputStream.flush();
             
               ouputStream.close();
             //  out.println("1");
           
        }
        catch(Exception e)
            {
                System.out.println("ERROR: "+e.getMessage()+" e: "+e.getLocalizedMessage()+"--"+e.toString());
                 // out.println("0");
            }
        */
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */

}
